#include "cube.h"

#include <fstream>
#include <string>
#include <sstream>
#include <math.h>

cube::cube(vec3 c) {

	cube::center=c;
	deg_x=deg_y=deg_z=0;
	
	// Add the first face (x,y,-z)
	C_Face *f1 = new C_Face();
	f1->color=vec3(0.01,0.01,0.01);
	f1->addVertex (vec3 (c[0]-AFC, c[1]-AFC, c[2]-AFC));
	f1->addVertex (vec3 (c[0]-AFC, c[1]+AFC, c[2]-AFC));
	f1->addVertex (vec3 (c[0]+AFC, c[1]+AFC, c[2]-AFC));
	f1->addVertex (vec3 (c[0]+AFC, c[1]-AFC, c[2]-AFC));
	cube::myFaces[B]=f1;

	// Add the second face (x,y, +z)

	C_Face *f2 = new C_Face();
	f2->color=vec3(0.01,0.01,0.01);
	f2->addVertex (vec3 (c[0]-AFC, c[1]-AFC, c[2]+AFC));
	f2->addVertex (vec3 (c[0]-AFC, c[1]+AFC, c[2]+AFC));
	f2->addVertex (vec3 (c[0]+AFC, c[1]+AFC, c[2]+AFC));
	f2->addVertex (vec3 (c[0]+AFC, c[1]-AFC, c[2]+AFC));
	cube::myFaces[F]=f2;

	// Add the third face (+x, y, z)

	C_Face *f3 = new C_Face();
	f3->color=vec3(0.01,0.01,0.01);
	f3->addVertex (vec3 (c[0]+AFC, c[1]-AFC, c[2]-AFC));
	f3->addVertex (vec3 (c[0]+AFC, c[1]+AFC, c[2]-AFC));
	f3->addVertex (vec3 (c[0]+AFC, c[1]+AFC, c[2]+AFC));
	f3->addVertex (vec3 (c[0]+AFC, c[1]-AFC, c[2]+AFC));
	cube::myFaces[R]=f3;
	
	// Add the fourth face (-x, y, z)

	C_Face *f4 = new C_Face();
	f4->color=vec3(0.01,0.01,0.01);
	f4->addVertex (vec3 (c[0]-AFC, c[1]-AFC, c[2]-AFC));
	f4->addVertex (vec3 (c[0]-AFC, c[1]+AFC, c[2]-AFC));
	f4->addVertex (vec3 (c[0]-AFC, c[1]+AFC, c[2]+AFC));
	f4->addVertex (vec3 (c[0]-AFC, c[1]-AFC, c[2]+AFC));
	cube::myFaces[L]=f4;

	C_Face *f5 = new C_Face();
	f5->color=vec3(0.01,0.01,0.01);
	f5->addVertex (vec3 (c[0]-AFC, c[1]+AFC, c[2]-AFC));
	f5->addVertex (vec3 (c[0]-AFC, c[1]+AFC, c[2]+AFC));
	f5->addVertex (vec3 (c[0]+AFC, c[1]+AFC, c[2]+AFC));
	f5->addVertex (vec3 (c[0]+AFC, c[1]+AFC, c[2]-AFC));
	cube::myFaces[T]=f5;

	C_Face *f6 = new C_Face();
	f6->color=vec3(0.01,0.01,0.01);
	f6->addVertex (vec3 (c[0]-AFC, c[1]-AFC, c[2]-AFC));
	f6->addVertex (vec3 (c[0]-AFC, c[1]-AFC, c[2]+AFC));
	f6->addVertex (vec3 (c[0]+AFC, c[1]-AFC, c[2]+AFC));
	f6->addVertex (vec3 (c[0]+AFC, c[1]-AFC, c[2]-AFC));
	cube::myFaces[D]=f6;
}

void cube::drawCube() {
	
	GLubyte halftone[] = {
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
    0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};


	glPushMatrix();
	
	glRotatef(deg_x, 1,0,0);
	glRotatef(deg_y, 0,1,0);
	glRotatef(deg_z, 0,0,1);
	glEnable(GL_POLYGON_STIPPLE);
	glPolygonStipple(halftone);

	for (int i=0; i<6; i++) {
		myFaces[i]->drawFace();
	}
	glDisable(GL_POLYGON_STIPPLE);
	glPushMatrix();
	glEnable(GL_LINE_STIPPLE);
	glLineWidth(3.95);
	glColor3f(0,0,0);
	glTranslatef(center[0],center[1],center[2]);
	glutWireCube(.28);
	glLineWidth(1);
	glDisable(GL_LINE_STIPPLE);
	glPopMatrix();
	glPopMatrix();
}